<?php
namespace App\Api\Fcjd;

use PhalApi\Api;


/**
 * Kami接口
 * @desc 读取phone_kami表数据
 */
class KamiAgent extends Api {
    public function getRules() {
        return array(
            'getList' => array(
                'page' => array('name' => 'page', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '页码'),
                'page_size' => array('name' => 'page_size', 'type' => 'int', 'min' => 1, 'max' => 500, 'require' => true, 'desc' => '每页数量'),
                'username' => array('name' => 'username', 'type' => 'string', 'require' => true, 'desc' => '用户名')
            ),
            'kamiUser' => array(
                'username' => array('name' => 'username', 'type' => 'string', 'require' => false, 'desc' => '用户名'),
                'nickname' => array('name' => 'nickname', 'type' => 'string', 'require' => false, 'desc' => '昵称'),
                'Invitation' => array('name' => 'Invitation', 'type' => 'string', 'require' => false, 'desc' => '邀请码'),
            ),
            'assignment' => array(
                'ids' => array('name' => 'ids', 'type' => 'array', 'format' => 'explode', 'separator' => ',', 'require' => true, 'desc' => '数组格式的ID列表'),
                'username' => array('name' => 'username', 'type' => 'string', 'desc' => '用户'),
                'user_id' => array('name' => 'user_id', 'type' => 'int', 'desc' => '用户ID'),
            )
        );
    }

    /**
     * 获取kami列表
     * @desc 获取phone_kami表数据
     * @return array items 数据列表
     * @return int total 总记录数
     */
    public function getList() {
        $page = $this->page;
        $pageSize = $this->page_size;

        $where = array(
            'username' => $this->username,
            'status' => 0,
        );
        
        $total = \PhalApi\DI()->notorm->phone_kami->where($where)->count();
        $items = \PhalApi\DI()->notorm->phone_kami
            ->select('card_code, note, user_id, nickname, username, duration_type, status, create_time, update_time')
            ->where($where)
            // ->order('kami_code DESC')
            ->limit(($page - 1) * $pageSize, $pageSize);
            
        \PhalApi\DI()->response->addResult('success', true);
        return array('items' => $items, 'total' => $total, 'page' => $page, 'page_size' => $pageSize);
    }
    
    /**
     * 获取kamiUser列表
     * @desc 获取phone_kami表数据
     * @return array items 数据列表
     * @return int total 总记录数
     */
    public function kamiUser() {

        $where = array(
            // 'Invitation' => $this->Invitation,
        );
        
        if( $this->username ){
            $where['username LIKE ?'] = '%'.$this->username.'%';
        }
        if( $this->nickname ){
            $where['nickname LIKE ?'] = '%'.$this->nickname.'%';
        }

        $items = \PhalApi\DI()->notorm->phalapi_user
            ->select('id,username,nickname')
            ->where($where)
            // ->order('kami_code DESC')
            ->limit(10);

        //由于username 是手机号，做为隐私保护，我需要把 手机号 进行加入**** 为 181****1234
        $newItems = array();
        foreach ($items as $key => $item) {
            $phone = $item['username'];
            $maskedPhone = substr_replace($phone, '****', 3, 4);
            $items[$key]['username'] = $maskedPhone;
            $newItems[$key] = array(
                'id' => $item['id'],
                'username' => $maskedPhone,
                'nickname' => $item['nickname'],
            );
        }
            
        \PhalApi\DI()->response->addResult('success', true);
        return $newItems;
    }
    
    /**
     * 分配卡密
     * @desc 批量更新卡密记录
     * @return array success 是否成功
     * @return array count 更新数量
     */
    public function assignment() {
        $ids = $this->ids;
        $userInfo = null;
        
        if (isset($this->user_id) && !empty($this->user_id)) {
            $userInfo = \PhalApi\DI()->notorm->phalapi_user->where('id', $this->user_id)->fetchOne();
        }
        
        $data = array();
        if (isset($this->note) && !empty($this->note)) {
            $data['note'] = $this->note;
        }
        if (isset($this->user_id) && !empty($this->user_id)) {
            $data['user_id'] = $this->user_id;
        }
        //每张卡密都关联用户信息
        if ($userInfo && isset($userInfo['nickname'])) {
            $data['nickname'] = $userInfo['nickname'];
        }
        if ($userInfo && isset($userInfo['username'])) {
            $data['username'] = $userInfo['username'];
        }
        
        if (empty($data)) {
            throw new \PhalApi\Exception\BadRequestException('没有可更新的数据');
        }
        
        $count = \PhalApi\DI()->notorm->phone_kami
            ->where('card_code', $ids)
            ->update($data);

        if ($count > 0) {
            foreach ($ids as $card_code) {
                $save_record = array(
                    'username' => $this->username,
                    'agent' => $userInfo['username'],
                    'agent_nickname' => $userInfo['nickname'],
                    'agent_id' => $userInfo['id'],
                    'card_code' => $card_code,
                    'create_time' => date('Y-m-d H:i:s', time()),
                );
                \PhalApi\DI()->notorm->phone_kami_user_record->insert($save_record);
            }
        }
            
        \PhalApi\DI()->response->addResult('success', true);
        return array('success' => true, 'count' => $count);
    }
}